<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{ data }" :tree-node="data" @delitDepts="editDepts" @delDepts="getDepartments" @addDepts="addDepts" />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>
<script>
import treeTools from './components/tree-tools.vue'
// 增删改查
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
import AddDept from './components/add-dept.vue'
export default {
  components: {
    treeTools,
    AddDept
  },
  // 使用directives注册v-focus指令
  data() {
    return {
      loading: false, // 用来控制进度弹层的显示和隐藏
      node: null,
      showDialog: false, // 显示窗体
      company: {},
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      }
    }
  },
  created() {
    this.getDepartments()// 调用自身
  },
  methods: {
    // 渲染数据
    async getDepartments() {
      this.loading = true
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(result.depts, '')
      this.loading = false
    },
    // 添加部门
    addDepts(node) {
      this.showDialog = true
      // 获取点击的节点
      this.node = node
    },
    // 编辑部门
    async editDepts(node) {
      this.showDialog = true
      // 用ref调用子组件的getDepartDetail接口请求
      await this.$refs.addDept.getDepartDetail(node.id) // 直接调用子组件中的方法 传入一个id
      this.node = node
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
